//
//  LXInputView.m
//  cat
//
//  Created by imera on 16/1/19.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXInputView.h"
#import "UIImage+Lx.h"

@interface LXInputView() <UITextFieldDelegate>

@property (nonatomic, copy) NSString *icon;

@property (nonatomic, copy) NSString *placeholder;
/** 输入外边框 */
@property (nonatomic, weak) UIImageView *inputImageView;
/** 输入图标 */
@property (nonatomic, weak) UIImageView *iconImageView;

@end

@implementation LXInputView

+ (instancetype)initWithIcon:(NSString *)icon placeholder:(NSString *)placeholder
{
    return [[self alloc] initWithIcon:icon placeholder:placeholder];
}

- (instancetype)initWithIcon:(NSString *)icon placeholder:(NSString *)placeholder
{
    self.icon = icon;
    self.placeholder = placeholder;
    self.isPwd = NO;
    return [self init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews
{
    UIImageView *inputImageView = [[UIImageView alloc]init];
    inputImageView.image = [UIImage resizeWithName:@"input"];
    [self addSubview:inputImageView];
    self.inputImageView = inputImageView;
    
    UIImageView *iconImageView = [[UIImageView alloc]init];
    iconImageView.image = [UIImage imageNamed:self.icon];
    [self addSubview:iconImageView];
    self.iconImageView = iconImageView;
    
    UITextField *textField = [[UITextField alloc]init];
    if (self.placeholder != nil) {
        textField.placeholder = self.placeholder;
    }
    textField.delegate = self;
    [self addSubview:textField];
    self.textField = textField;
}

- (void)layoutSubviews
{
    UIImageView *inputImageView = self.inputImageView;
    CGFloat inputImageViewX = 0;
    CGFloat inputImageViewY = 0;
    CGFloat inputImageViewW = self.frame.size.width;
    CGFloat inputImageViewH = self.frame.size.height;
    inputImageView.frame = CGRectMake(inputImageViewX, inputImageViewY, inputImageViewW, inputImageViewH);
    
    UIImageView *iconImageView = self.iconImageView;
    CGFloat iconImageViewH = 25;
    CGFloat iconImageViewW = iconImageViewH;
    CGFloat iconImageViewY = (self.frame.size.height - iconImageViewH) / 2;
    CGFloat iconImageViewX = iconImageViewY;
    iconImageView.frame = CGRectMake(iconImageViewX, iconImageViewY, iconImageViewW, iconImageViewH);
    
    UITextField *textField = self.textField;
    textField.secureTextEntry = self.isPwd;
    textField.tintColor = [UIColor grayColor];
    CGFloat textFieldX = CGRectGetMaxX(iconImageView.frame) + 10;
    CGFloat textFieldY = 0;
    CGFloat textFieldH = self.frame.size.height;
    CGFloat textFieldW = self.frame.size.width - textFieldX - 10;
    textField.frame = CGRectMake(textFieldX, textFieldY, textFieldW, textFieldH);
    
}

- (void)hideKeyBorard
{
    [self.textField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
@end
